import * as types from '../mutation-types.js'
import apiurl from '@/api/apiurl'
// import Api from '@/api/index'

//全局状态
const state = {
  username: '',
  password: '',
  tabData: [],
  treeData: [],
  token: ''
}

//getters
const getters = {
  username: state => {
    return state.username
  },
  password: state => {
    return state.password
  },
  tabData: state => {
    return state.tabData
  },
  treeData: state => {
    return state.treeData
  },
  token: state => {
    return state.token
  },
}

const mutations = {
  [types.SET_USERNAME] (state, username) {
    state.username = username
  },
  [types.SET_PASSWORD] (state, password) {
  	state.password = password
  },
  [types.SET_TABDATA](state, tabData) {
    state.tabData = tabData
  },
  [types.SET_TREEDTA](state, treeData) {
    state.treeData = treeData
  },
  [types.SET_TOKEN](state, token) {
    state.token = token
  }
}

const actions = {
  /**
   * 获取群组列表
   * @param { parentId: 父id }
   * @returns { status: 1, result: { gpsGroupList: [{}] }, message: '请求成功' }
   */
  async fetchListGroup(_, params = {}) {
    // console.log(params,'获取群组列表参数')
    // console.log(apiurl.listGroup)
    let res = await Api.request(apiurl.listGroup, {
      ...params
    })
    return res
  },

  /**
   * 添加群组
   * @param { groupName: 群组名称, parentId: 父id }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddGroup(_, params = {}) {
    // console.log(params,'添加群组参数')
    // console.log(apiurl.addGroup)
    let res = await Api.request(apiurl.addGroup, {
      ...params
    })
    return res
  },

  /**
   * 修改群组名称
   * @param { groupName: 新的群组名称, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '修改成功' }
   */
  async fetchEditGroup(_, params = {}) {
    // console.log(params,'修改群组参数')
    // console.log(apiurl.editGroup)
    let res = await Api.request(apiurl.editGroup, {
      ...params
    })
    console.log(res)
    return res
  },

  /**
   * 获取子账号列表
   * @param { parentId: 父id }
   * @returns { status: 1, result: { gpsUserList: [{}] }, message: '请求成功' }
   */
  async fetchChildList(_, params = {}) {
    // console.log(params,'获取子账号列表参数')
    // console.log(apiurl.userBypassList)
    let res = await Api.request(apiurl.userBypassList, {
      ...params
    })
    return res
  },

  /**
   * 添加子账号
   * @param { privateKey: 私钥, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '添加子账号成功' }
   */
  async fetchAddChildAccount(_, params = {}) {
    // console.log(params,'添加子账号参数')
    // console.log(apiurl.addBypassAccount)
    let res = await Api.request(apiurl.addBypassAccount, {
      ...params
    })
    return res
  },

  /**
   * 修改用户类型
   * @param { userId: id, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '修改成功' }
   */
  async fetchUpdateAccountType(_, params = {}) {
    // console.log(params,'修改用户类型参数')
    // console.log(apiurl.updateAccountType)
    let res = await Api.request(apiurl.updateAccountType, {
      ...params
    })
    return res
  },

  /**
   * 群组删除设备
   * @param { facilityId: 设备id, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '删除成功' }
   */
  async fetchDelUserFacility(_, params = {}) {
    // console.log(params,'群组删除设备参数')
    // console.log(apiurl.delUserFacility)
    let res = await Api.request(apiurl.delUserFacility, {
      ...params
    })
    return res
  },

  /**
   * 群组添加设备
   * @param { facilityId: 设备id, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddUserFacility(_, params = {}) {
    // console.log(params,'群组添加设备参数')
    // console.log(apiurl.addUserFacility)
    let res = await Api.request(apiurl.addUserFacility, {
      ...params
    })
    return res
  },

  /**
   * 群组删除网盘
   * @param { dishId: 网盘id, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '删除成功' }
   */
  async fetchDelUserDish(_, params = {}) {
    // console.log(params,'群组删除网盘参数')
    // console.log(apiurl.delUserDish)
    let res = await Api.request(apiurl.delUserDish, {
      ...params
    })
    return res
  },

  /**
   * 群组添加网盘
   * @param { dishId: 网盘id, groupId: 群组id }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddUserDish(_, params = {}) {
    // console.log(params,'群组添加网盘参数')
    // console.log(apiurl.addUserDish)
    let res = await Api.request(apiurl.addUserDish, {
      ...params
    })
    return res
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
